﻿using Engine;
using Game;
using GameEntitySystem;
using System;
using System.Globalization;

namespace JackODummy
{
	public class JackOModLoader : ModLoader
	{
		public override void __ModInitialize()
		{
			ModsManager.RegisterHook("OnProjectLoaded", this);
			ModsManager.RegisterHook("ProcessAttackment", this);
		}
		public override void OnProjectLoaded(Project project)
		{
			JackOUtility.LoadSubsystems(project);
		}
		public override void ProcessAttackment(Attackment attackment)
		{
			if (attackment == null)
				return;
			ComponentJackODummy componentJackODummy = attackment.Target.FindComponent<ComponentJackODummy>();
			if (componentJackODummy != null)
			{
				string text2 = (0f - attackment.AttackPower).ToString("0", CultureInfo.InvariantCulture);
				Vector3 hitValueParticleVelocity = attackment.Attacker?.FindComponent<ComponentBody>()?.Velocity ?? Vector3.Zero;
				HitValueParticleSystem particleSystem = new(attackment.HitPoint + (0.75f * attackment.HitDirection), (1f * attackment.HitDirection) + hitValueParticleVelocity, Color.White, text2);
				JackOUtility.SubsystemParticles.AddParticleSystem(particleSystem);
				componentJackODummy.WasHit = true;
				componentJackODummy.StunTime = attackment.StunTimeSet ?? componentJackODummy.StunTime + attackment.StunTimeAdd;
				float direction = Vector3.Dot(attackment.TargetBody.Matrix.Forward, attackment.HitDirection) > 0 ? 1f : -1f;
				componentJackODummy.ImpulseOrder = attackment.ImpulseFactor * direction;
				attackment.ImpulseFactor = 0f;
				attackment.AttackPower = 0f;
				attackment.StunTimeSet = 0f;
				componentJackODummy.SubsystemParticles.AddParticleSystem(new BlockDebrisParticleSystem(componentJackODummy.SubsystemTerrain, attackment.HitPoint, 0.4f, 0.4f, Color.White, debrisTextureSlots[Random.Int(0, debrisTextureSlots.Length - 1)]));
				componentJackODummy.SubsystemAudio.PlayRandomSound(componentJackODummy.ComponentDamage.DamageSoundName, 1f, 0f, attackment.HitPoint, 4f, autoDelay: true);
			}
		}

		static readonly Game.Random Random = new();
		static readonly int[] debrisTextureSlots = [
			47,//木棍
			95,//稻草
			102,//南瓜
			213//布
		];
	}
}
